package com.bi.onlineEducation.aspect;

import com.alibaba.fastjson.JSONObject;
import com.bi.onlineEducation.constant.OEConstant;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

@Aspect
@Component
@Slf4j
public class WebProcessAspect {

    @Pointcut("@annotation(com.bi.onlineEducation.annotation.WebProcess)")
    public void webProcess() {
    }

    /**
     * 转换参数
     *
     * @param joinPoint
     */
    @Before("webProcess()")
    public void around(JoinPoint joinPoint) {
        ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attrs.getRequest();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        //拼接类名加方法
        String declaring = method.getDeclaringClass().getName() + "." + method.getName();
        log.info("<=====================================================>");
        log.info("请求方法：{}-->方法调用开始", declaring);
        log.info("请求时间：{}", OEConstant.SDF_Y_M_D_H_M_S.format(new Date()));
        log.info("请求URL：{}", request.getRequestURL().toString());
        log.info("请求方式：{}", request.getMethod());
        log.info("请求入参：{}", JSONObject.toJSONString(joinPoint.getArgs()));
        log.info("------------------------------------------------------");
    }


}
